package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.admin.pojo.dto.RaidersDetailDTO;
import com.xbongbong.admin.pojo.dto.RaidersListDTO;
import com.xbongbong.admin.pojo.dto.RaidersOperationDetailDTO;
import com.xbongbong.admin.pojo.dto.RaidersRoleListDTO;
import com.xbongbong.admin.pojo.vo.RaidersDetailVO;
import com.xbongbong.admin.pojo.vo.RaidersListVO;
import com.xbongbong.admin.pojo.vo.RaidersOperationDetailVO;
import com.xbongbong.admin.service.RaidersService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zcp
 * @version v1.0
 * @date 19/9/27 027 14:27
 * @update 19/9/27 027 14:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/raiders")
public class RaidersController {
    @Resource
    private RaidersService raidersService;

    /**
     * 攻略列表
     * @param raidersListDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/9/27 027 14:30
     * @update 19/9/27 027 14:30
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody RaidersListDTO raidersListDTO, BindingResult br) {
        XbbResponse<RaidersListVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersListVO raidersListVO = raidersService.list(raidersListDTO);
                response = new XbbResponse<>(raidersListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 首页角色轮播图攻略列表
     * @param raidersRoleListDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/9/27 027 14:30
     * @update 19/9/27 027 14:30
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/role/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String roleList(@RequestBody RaidersRoleListDTO raidersRoleListDTO, BindingResult br) {
        XbbResponse<RaidersListVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                raidersRoleListDTO.setGroupId(ProBaseConfig.getProRaidersGroupId());
                RaidersListDTO raidersListDTO = new RaidersListDTO();
                BeanUtil.copyProperties(raidersRoleListDTO, raidersListDTO);
                RaidersListVO raidersListVO = raidersService.list(raidersListDTO);
                raidersService.removeRaider(raidersListVO);
                response = new XbbResponse<>(raidersListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 攻略详情
     * @param raidersDetailDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/9/29 029 16:54
     * @update 19/9/29 029 16:54
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody RaidersDetailDTO raidersDetailDTO, BindingResult br) {
        XbbResponse<RaidersDetailVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersDetailVO raidersDetailVO = raidersService.detail(raidersDetailDTO);
                response = new XbbResponse<>(raidersDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 攻略详情-实操使用
     * @param raidersOperationDetailDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @date 19/9/30 030 9:48
     * @update 19/9/30 030 9:48
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/operation/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String operationDetail(@RequestBody RaidersOperationDetailDTO raidersOperationDetailDTO, BindingResult br) {
        XbbResponse<RaidersOperationDetailVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                RaidersOperationDetailVO raidersOperationDetailVO = raidersService.operationDetail(raidersOperationDetailDTO);
                response = new XbbResponse<>(raidersOperationDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
